/*
@Time : 2021/2/5 16:17
@Author : 吴周海
@Mail: wzh200x@126.com
@File : apiRequest.go
@Software: GoLand
*/
package game

import (
	"GameApiServer/models"
	"encoding/json"
	"github.com/beego/beego/v2/client/httplib"
	"github.com/beego/beego/v2/core/logs"
	"strconv"
	"strings"
)

func RequestGameServer(msg *GameApiMessage) (bool, error) {
	sid, _ := strconv.Atoi(msg.Sid)
	sInfo := models.GameServerInfo{
		ServerId: sid,
	}
	err := sInfo.Read()
	if err == nil {
		reqUrl := "http://" + sInfo.IpHost + ":" + sInfo.MasterPort + "?" + msg.FormatReUrl()
		req := httplib.Get(reqUrl)

		datar, rErr := req.String()
		datar = strings.Trim(datar, "\u0001\u0004")
		datar = strings.Trim(datar, "\u0002")
		if rErr != nil {
			return false, rErr
		}
		var dat map[string]interface{}
		if err := json.Unmarshal([]byte(datar), &dat); err == nil {
			if value, ok := dat["s"]; ok {
				if int(value.(float64)) == 1 || int(value.(float64)) == -3 {
					return true, nil
				}
			}
		}
		logs.Debug(datar)
		logs.Debug(dat)
	}
	return true, err
}
